package com.jc.repository.configure;

import com.jc.repository.mapper.intercepor.MapperAspectConfig;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.ClassPathMapperScanner;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.*;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.UrlResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.net.URL;
import java.util.*;

@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages="com.jc.com.jc.repository.shard.table")
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@ConditionalOnBean(DataSource.class)
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@Import({MapperAspectConfig.class})
public class MybatisAutoConfiguration {
	
	private static Log log = LogFactory.getLog(MybatisAutoConfiguration.class);

	  @Autowired
	  private MybatisProperties properties;

	  @Autowired(required = false)
	  private Interceptor[] interceptors;

	  @Autowired
	  private ResourceLoader resourceLoader = new DefaultResourceLoader();

	  @Autowired(required = false)
	  private DatabaseIdProvider databaseIdProvider;

	  @PostConstruct
	  public void checkConfigFileExists() {
	    if (this.properties.isCheckConfigLocation() && StringUtils.hasText(this.properties.getConfigLocation())) {
	      Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation());
	      Assert.state(resource.exists(), "Cannot find config location: " + resource
	          + " (please add config file or check your Mybatis configuration)");
	    }
	  }
	  
	  
	  @Bean
	  @ConditionalOnMissingBean
	  public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
	    SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
	    factory.setDataSource(dataSource);
	    factory.setVfs(SpringBootVFS.class);
	    if (StringUtils.hasText(this.properties.getConfigLocation())) {
	      factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
	    }
	    factory.setConfiguration(properties.getConfiguration());
	    if (!ObjectUtils.isEmpty(this.interceptors)) {
	      factory.setPlugins(this.interceptors);
	    }
	    if (this.databaseIdProvider != null) {
	      factory.setDatabaseIdProvider(this.databaseIdProvider);
	    }
	    if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
	      factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
	    }
	    if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
	      factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
	    }
	    if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
	      factory.setMapperLocations(this.properties.resolveMapperLocations());
	    }

	    return factory.getObject();
	  }

	  @Bean
	  @ConditionalOnMissingBean
	  public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
	    ExecutorType executorType = this.properties.getExecutorType();
	    if (executorType != null) {
	      return new SqlSessionTemplate(sqlSessionFactory, executorType);
	    } else {
	      return new SqlSessionTemplate(sqlSessionFactory);
	    }
	  }

	  /**
	   * This will just scan the same base package as Spring Boot does. If you want
	   * more power, you can explicitly use
	   * {@link org.mybatis.spring.annotation.MapperScan} but this will get typed
	   * mappers working correctly, out-of-the-box, similar to using Spring Data JPA
	   * repositories.
	   */
	  public static class AutoConfiguredMapperScannerRegistrar
	      implements BeanFactoryAware, ImportBeanDefinitionRegistrar, ResourceLoaderAware {
		 
		public static final String MAPPER_BASEPACK_LOCATION = "mapper-base-packages.properties";
		  
	    private BeanFactory beanFactory;

	    private ResourceLoader resourceLoader;

	    @Override
	    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

	      log.debug("Searching for mappers annotated with @Mapper'");

	      ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);

	      try {
	        if (this.resourceLoader != null) {
	          scanner.setResourceLoader(this.resourceLoader);
	        }

	        //List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
	        Set<String> packages = getMapperBasePackages();
	        if (log.isDebugEnabled()) {
	          for (String pkg : packages) {
	            log.debug("Using auto-configuration base package '" + pkg + "'");
	          }
	        }
	        if(packages ==null || packages.isEmpty()){
	        		return;
	        }
	        scanner.setAnnotationClass(Mapper.class);
	        scanner.registerFilters();
	        scanner.doScan(StringUtils.toStringArray(packages));
	      } catch (IllegalStateException ex) {
	        log.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.");
	      }
	    }

	    @Override
	    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
	      this.beanFactory = beanFactory;
	    }

	    @Override
	    public void setResourceLoader(ResourceLoader resourceLoader) {
	      this.resourceLoader = resourceLoader;
	    }
	    
	    public static Set<String> getMapperBasePackages(){
			  ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
			  Set<String> basePackages = new HashSet<String>();
			  try {
				Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(MAPPER_BASEPACK_LOCATION) :
						ClassLoader.getSystemResources(MAPPER_BASEPACK_LOCATION));
				
				while (urls.hasMoreElements()) {
					URL url = urls.nextElement();
					Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
					String basePackage = properties.getProperty("mapper.base.package");
					if(!StringUtils.isEmpty(basePackage)){
						basePackages.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(basePackage)));
					}				
				}
			} catch (Exception e) {
				
			}
			return basePackages;
		  }
	  }

	  /**
	   * {@link org.mybatis.spring.annotation.MapperScan} ultimately ends up
	   * creating instances of {@link MapperFactoryBean}. If
	   * {@link org.mybatis.spring.annotation.MapperScan} is used then this
	   * auto-configuration is not needed. If it is _not_ used, however, then this
	   * will bring in a bean registrar and automatically register components based
	   * on the same component-scanning path as Spring Boot itself.
	   */
	  @Configuration
	  @Import({ AutoConfiguredMapperScannerRegistrar.class })
	  //@ConditionalOnMissingBean(MapperFactoryBean.class)
	  public static class MapperScannerRegistrarNotFoundConfiguration {

	    //@PostConstruct
	    public void afterPropertiesSet() {
	      if (log.isDebugEnabled()) {
	        log.debug(String.format("No %s found.", MapperFactoryBean.class.getName()));
	      }
	    }
	  }
}
